package com.zhiyou100.javaweb.myservlet.homework.day004.studentmanagementsystem;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @packageName: javase_26
 * @className: ${NAME}
 * @Description: TODO 根据id获取一个
 * @author: yang
 * @date: 2020/5/26
 */
@WebServlet(name = "Day004ServletGetOne", urlPatterns = "/day04/getOne")
public class Day004ServletGetOne extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 1 设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 2 设定类型
        StudentDao studentDao = new StudentDao();
        // 3 创建dao对象
        String queryId = request.getParameter("queryId");
        Enumeration<String> attributeNames = request.getAttributeNames();
        // 4 获取参数
        boolean b = studentDao.queryOne(Integer.parseInt(queryId));
        // 5 判断该id是否存在
        PrintWriter writer = response.getWriter();
        // 6 获取writer流
        String message = "";
        // 7 定义一个字符串记录信息

        if (b) {
                // 如果存在
            Student one = studentDao.getOne(Integer.parseInt(queryId));
            // 获取一个
            message = "<h1> id: " + one.getStudentId() + " name: " + one.getStudentName() + " gender: " + one.getStudentGender() + " Score:" + one.getStudentScore() + "</h1>";
        } else {
            message = "<h1>该用户不存在</h1>";
        }
        writer.write(message);
        // 写入
        writer.flush();
        // 刷新
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
